﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ControlExs;

namespace WSNs
{
    public partial class SampleManagement : FormEx
    {
        private readonly NodesData _nodesData = NodesData.GetInstance();
        private Form1 _formMain;
        private int _statusFlag = 0;

        private List<byte[]> _listToSend = new List<byte[]>();
        private int _sendFileIndex = 0;
        public int GetTestSize;

        public SampleManagement(Form1 fm)
        {
            InitializeComponent();
            _formMain = fm;
        }

        private void SampleManagement_Load(object sender, EventArgs e)
        {
            cb_faulttype.SelectedIndex = 0;
            btn_ok.Focus();
        }

        private void SampleManagement_Shown(object sender, EventArgs e)
        {
            if (_nodesData.GetShowNodes().Count == panel_filelist.Controls.Count) return;
            panel_filelist.Controls.Clear();
            ShowList();
        }

        public void FusionIsOk(int nodeID, int flag)
        {
            int isAll = 0;
            foreach (ListBox l in panel_filelist.Controls)
            {
                if (l.Items[0].ToString().Last() != '√') //判断是否全部处理完
                    isAll++;
                if (nodeID.ToString() != l.Name) continue;
                l.SelectedIndex = 0;
                l.Items[0] = "Node" + nodeID + " √"; //融合完成标志               
            }
            if(isAll == panel_filelist.Controls.Count)
                _formMain.GetDataLister().SendCMD(new byte[] { 0xcc, 0xdd, 0xeb, 0x04, 0, 72, 16, 0x00, 0x00, 0x00 });
        }

        /// <summary>
        /// 更新文件名目录
        /// </summary>
        /// <param name="nodeID">指定的节点</param>
        /// <param name="dir">新增的文件名</param>
        public void UpdataDir(int nodeID, string dir)
        {
            if (dir.Substring(0,1)=="T")
            {
                return;
            }
            foreach (ListBox l in panel_filelist.Controls)
            {
                if (nodeID.ToString() != l.Name) continue;
                if (!l.Items.Contains(dir))
                {
                        l.Items.Add(dir);

                }
            }
        }

        /// <summary>
        /// 发送一次文件名，由串口返参数决定是否再次发送
        /// </summary>
        public void SendFileName()
        { 
            _formMain.GetDataLister().SendCMD(_listToSend[_sendFileIndex]);
            if (_listToSend[_sendFileIndex][10] == 0)
            {
                _sendFileIndex = 0;
            }
            else
            {
                _sendFileIndex++;
            }
        }

        private void ShowList()
        {
            List<int> lNode = _nodesData.GetShowNodes();
            int tempWidth = 0;
            foreach (int i in lNode)
            {
                ListBox lst = new ListBox();
                lst.Name = i.ToString();
                lst.BackColor = Color.Gray;
                lst.Font = new Font("Consolas", 11);
                lst.Location = new Point(tempWidth++ * 113, 0);
                lst.Width = 115;
                lst.Height = panel_filelist.Height - 18;
                lst.Items.Add("Node:" + i.ToString());
                panel_filelist.Controls.Add(lst);
            }
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {

        }

        private void btn_ok_Click(object sender, EventArgs e)
        {
            //_formMain.GetDataLister().SendCMD()
            GetTestSize = int.Parse(tb_sampleCount.Text);
            if (Text=="选择训练样本")
            {
                foreach (ListBox l in panel_filelist.Controls)
                {
                    try
                    {
                        string s = l.SelectedItem.ToString();
                    }
                    catch
                    {
                        MessageBox.Show("请选择文件");
                        return;
                    }
                }
                timer_status.Enabled = true;
                List<byte> lb = new List<byte>();
                _listToSend.Clear();
                foreach (ListBox l in panel_filelist.Controls)
                {
                    lb.Clear();
                    string s = l.SelectedItem.ToString();
                    if (s.Substring(0, 4) == "Node") continue;
                    lb.AddRange(new byte[] { 0xCC, 0xCE, (byte)(s.Length + 4), 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }); //添加预处理信息
                    lb.Add(1); //是否还有下一组数据标志
                    lb.Add(byte.Parse(l.Name)); //节点ID
                    lb.AddRange(Encoding.ASCII.GetBytes(s)); //数据
                    lb.Add(byte.Parse(tb_sampleCount.Text)); //参数1
                    lb.Add((byte)cb_faulttype.SelectedIndex); //参数2
                    _listToSend.Add(lb.ToArray()); //添加需要发送的文件名、样本数、类型
                }
                if (_listToSend.Count == 0) return;
                _listToSend.Last()[10] = 0;
                SendFileName();
            }
            else
            {
                foreach (ListBox l in panel_filelist.Controls)
                {
                    try
                    {
                        string s = l.SelectedItem.ToString();
                    }
                    catch
                    {
                        MessageBox.Show("请选择文件");
                        return;
                    }
                }
                timer_status.Enabled = true;
                List<byte> lb = new List<byte>();
                _listToSend.Clear();
                foreach (ListBox l in panel_filelist.Controls)
                {
                    lb.Clear();
                    string s = l.SelectedItem.ToString();
                    if (s.Substring(0, 4) == "Node") continue;
                    lb.AddRange(new byte[] { 0xCC, 0xCE, (byte)(s.Length + 4), 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }); //添加预处理信息
                    lb.Add(1); //是否还有下一组数据标志
                    lb.Add(byte.Parse(l.Name)); //节点ID
                    lb.AddRange(Encoding.ASCII.GetBytes(s)); //数据
                    lb.Add(byte.Parse(tb_sampleCount.Text));
                    lb.Add(10); //参数2
                    _listToSend.Add(lb.ToArray()); //添加需要发送的文件名、样本数、类型
                }
                if (_listToSend.Count == 0) return;
                _listToSend.Last()[10] = 0;
                SendFileName();

            }
            
        }

        private void btn_refreshDir_Click(object sender, EventArgs e)
        {
            panel_filelist.Controls.Clear();
            ShowList();
            _formMain.GetDataLister().SendCMD(new byte[] { 0xCC, 0xDD, 0xEB, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 });
            foreach (int i in _nodesData.GetShowNodes())
            {
                _formMain._fusionDataIsSaved[i] = false;
                _nodesData.ClearFusionData(i);
            }
        }

        private void timer_status_Tick(object sender, EventArgs e)
        {
            if (_statusFlag == 0)
            {
                label_status.Text = "状态：训练中.";
                _statusFlag = 1;
            }
            else if (_statusFlag == 1)
            {
                label_status.Text = "状态：训练中..";
                _statusFlag = 2;
            }
            else if (_statusFlag == 2)
            {
                label_status.Text = "状态：训练中...";
                _statusFlag = 0;
            }
        }

        private void SampleManagement_FormClosing(object sender, FormClosingEventArgs e)
        {

        }

        private void SampleManagement_FormClosed(object sender, FormClosedEventArgs e)
        {

        }
    }
}
